<?php

namespace App\Http\Controllers\Site;


use App\B2BProject\Area;
use App\B2BProject\Custom\Business\Goods;
use App\B2BProject\Custom\Business\Issue;
use App\B2BProject\Custom\Business\Store;
use App\B2BProject\Custom\Member;
use App\B2BProject\Site\HelperControllerBuildTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class GoodsController extends Controller
{
    use HelperControllerBuildTrait;

    /**
     * 商品详情页
     * @param Request $request
     * @param $id
     * @return $this
     */
    public function index(Request $request, $id)
    {

        $goods     = Goods::where('id', $id)->where('strict_lock',false)->first();
        $authorize = $goods->isGranted();

        $store       = $goods->store;
        $information = $store->member->information;
        $member      = Member::where('id', $store->member_id)->first();

        //商品的规格
        if ($goods->hasSpecification()) {
            $goodsSpecifications = $goods->goodsSpecification;
        } else {
            $goodsSpecifications = [];
        }

        return view('host.goodsdetail')->with('storeInfo', $store)->with('information',
            $information)->with('menberinfo',
            $member)->with('goodsSpecifications',
            $goodsSpecifications)->with('authorize', $authorize)->with('goods', $goods);
    }


    /**
     * 商品详情页面的信息  通过ajax请求 返回数据
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(Request $request)
    {
        //id为商品的id
        $goods_id  = $request->input('goods_id');
        $goods     = Goods::where('id', $goods_id)->first();
        $authorize = $goods->isGranted();  // 判断是否验证

        $store       = $goods->store;
        $information = $store->member->information;
        $member      = Member::where('id', $store->member_id)->first();
        $data        = [];

        if ($goods->hasSpecification()) {//商品的规格
            $goodsSpecifications = $goods->goodsSpecification;
            $data['specid']      =
                $goodsSpecifications->spec_first . '/' . $goodsSpecifications->spec_second . '/' . $goodsSpecifications->spec_third . '/' . $goodsSpecifications->spec_forth . '/' . $goodsSpecifications->spec_fifth;
        } else {
            $goodsSpecifications = [];
            $data['specid']      = '';
        }

        //返回数据
        $data['minimum_order'] = $goods->minimum_order;
        $data['code']          = $goods->id;
        $data['name']          = $goods->name;
        $data['stock']         = $goods->stock;
        $data['image']         = $goods->goodsImage()->first()->image;
        $data['images']        = $goods->image;
        $data['addr']          =
            Area::getName($goods->province_id) . ' ' . Area::getName($goods->city_id) . ' ' . Area::getName($goods->county_id);

        if ($goods->grant_status == '0') { // 0  自由集散区商品 显示单价
            $data['price'] = '￥ ' . $goods->original_price;
        } else {
            if (\Auth::check()) { //  已登陆状态
                if (\Auth::user()->class == 2) {  // 采购商身份
                    if ($goods->isGranted()) {//判断商品是否授权
                        $data['price'] = '￥ ' . $goods->original_price * $goods->getPriceStrategy();
                    } else {
                        $data['price'] = '未授权';
                    }
                } else {  //供应商身份
                    $member = \Auth::user();

                    if (Goods::where('store_id',
                        Store::where('member_id', $member->id)->first()->id)->first()
                    ) {   // 供应商有自己的店铺
                        if (Goods::where('store_id',
                                Store::where('member_id',
                                    $member->id)->first()->id)->first()->store_id == $goods->store->id
                        ) { //供应商自己的店铺
                            $data['price'] = '￥ ' . $goods->original_price;
                        } else {
                            $data['price'] = '未授权';
                        }
                    } else {
                        $data['price'] = '未授权';
                    }
                }
            } else {//未登陆状态
                if ($goods->grant_status == '0') { // 1 是从供采分销区过来的  显示单价
                    $data['price'] = '￥ ' . $goods->original_price * $goods->getPriceStrategy();
                } else {//供采分销区过来的  显示请登录后查看
                    $data['price'] = '请登录后查看';
                }
            }
        }
        $item['data'][] = $data;
        return response()->json($item);
    }


    /**
     * 商品评论
     * @param $id
     */
    public function comment($id)
    {

    }

    /**
     * 采购商对商品的咨询
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function consult(Request $request)
    {
        $member = \Auth::user();
        try {
            \DB::transaction(function () use ($request, $member) {

                $validator = Validator::make($request->all(), [
                    'content' => 'required', // 咨询内容
                    'goodsId' => 'required|numeric', //商品id
                    'userId'  => 'required|numeric', // 用户id
                ]);

                if ($validator->fails()) {
                    return redirect()->back();
                }

                //是否匿名
                if ($request->input('cryptonym') == 1) {
                    $data = [
                        'goods_id' => $request->input('goodsId'),
                        'content'  => $request->input('content'),
                        'anonymity'  => '1',
                        'member_id' => $member->id, //暂时存在
                    ];

                } else {
                    $data = [
                        'goods_id'  => $request->input('goodsId'),
                        'content'   => $request->input('content'),
                        'member_id' => $member->id,
                    ];
                }
                Issue::create($data)->save();
            });
            return response()->json('ok');
        } catch (ModelNotFoundException $e) {
            return response('error', 500);
            throw($e);
        }
    }
}
